
import config from '../../../common/scripts/config.js'
// import Request from '../../../common/scripts/request.js'
// import Request from '../../../common/scripts/request/dev-request.js'
import Request from '../../../common/scripts/request/pro-request.js'
// import Request from '../../../common/scripts/request/test-request.js'
/**
 * 业务方法的封装类
 * @author 周亮
 * @createDate 2019-05-22
 * @notice
 */
class IndexService {
	
	constructor() {
		this.request = new Request()
		this.config = config
	}
	
	test(options) {
		return this.request.get('/', options, 'local')
	}
	/**
	 * 用户系统登录授权方法
	 * @param {Object} options
	 */
	sysLogin(options) {
		let obj = {
			'phone': options.phone,
			'union_id': options.union_id,
			'nickname': options.nickname,
			'avatar': options.avatar,
			'wxapp_openid': options.wxapp_openid,
			'token': (options.phone * 3 - 24) * 6 - 158
		}
		return this.request.post('/appapi.php?g=Appapi&c=AppUser&a=mini_reg', obj, 'test')
	}
	
	/**
	 * 微信登录授权接口
	 * @param {Object} options
	 */
	login(options) {
		let that = this
		let p = new Promise(function(resolve, reject) {
			uni.login({
				provider: 'weixin',
				success: (login_res => {
					resolve(login_res)
				}),
				fail: (err => {
					reject(err)
				})
			})
		})
		return p
	}
	/**
	 * 获取用户信息
	 */
	getUserInfo() {
		let p = new Promise(function(resolve, reject) {
			uni.getUserInfo({
				provider: 'weixin',  
				withCredentials: true,
				success: function(result) {
					resolve(result)
				},
				fail: function(err) {
					reject(err)
				}
			})
		})
		return p
	}
	/**
	 * 获取用户信息接口
	 * @param {Object} options 对应的参数
	 */
	getInfo(options) {
		return this.request.post('/appapi.php?g=Appapi&c=Wxapp&a=login', options, 'test')
	}
	
	/**
	 * 获取 UnionID 方法
	 * @param {Object} options
	 */
	getUnionID(options) {
		let that = this
		let url = that.config.unionURL + '?access_token=' + options.access_token + '&openid=' + options.openid
		return this.request.sendGet(url, {})
	}
	
	/**
	 * 获取 AccessToken
	 * @param {Object} options
	 */
	getAccessToken(options) {
		let that = this
		let url = that.config.accessURL + '?grant_type=client_credential&appid=' + that.config.appid + '&secret=' + that.config.secret
		return this.request.sendGet(url, {})
	}
	
	/**
	 * 获取 openid
	 * @param {Object} options
	 */
	getOpenID(options) {
		let that = this
		let code = options.code
		let url = that.config.openURL + '?appid=' + that.config.appid + '&secret=' + that.config.secret + '&js_code=' + code + '&grant_type=authorization_code'
		return this.request.sendGet(url, {})
	}
	
	clear() {
		let url = 'https://www.ljyjy.top/appapi.php?g=Appapi&c=AppUser&a=show_session'
		return this.request.sendGet(url, {})
	}
	
	getOpenIDPhp(options) {
		let that = this
		return this.request.get('/appapi.php?g=Appapi&c=AppComm&a=get_openid', { code: options.code }, 'test')
	}
	
	getGoods(options) {
		return this.request.get('/appapi.php?g=Appapi&c=AppComm&a=get_goods_info', options, 'test')
	}
	
	requestRegion(options) {
		return this.request.get('/appapi.php?g=Appapi&c=AppComm&a=config', options, 'test')
	}

}

export default IndexService
